﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data.OleDb;

/// <summary>
/// 本页面用于处理登陆后用户的阅读请求、收藏、以及工作认证等操作。
/// 页面传入参数说明
/// DataNo  必选
/// OP      可选                    *表示需要对数据库进行操作
///         UpdateViewTimes 可选    *更新阅读次数，如果用户登录，还会在用户已阅读记录中+1
///         RequestConfirm  可选    *表示用户申请工作确认，需要用户登录
///         OpFavor         可选    *表示用户将本记录添加入收藏或从收藏中移除，需要用户登录
/// </summary>


public partial class ajax_MiscProcess : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string dataNo = Request.QueryString["DataNo"] != null ? Request.QueryString["DataNo"].ToString() : "";
        string empID = Session["EmpID"] != null ? Session["EmpID"].ToString() : "";

        //string dataNo = "1";
        //string empID = "62117";
        //如果页面传入参数不带dataNo就不输出任何值，直接返回
        if (dataNo == "" )
        {
            return;
        }
        //如果传入参数带有op表示有操作
        if(null != Request.QueryString["OP"])
        {
            //如果传入参数带有UpdateViewTimes就把对应的阅读数量+1
            if(null != Request.QueryString["UpdateViewTimes"])
                UpdateViewTime(dataNo, empID);
            if (empID != "")
            {
                //带有RequestConfirm就表示申请工作认证
                if (null != Request.QueryString["RequestConfirm"])
                    RequestConfirm(dataNo, empID);
                //带有OpFavor就将本内容添加到收藏夹里面或如果存在这个收藏就取消
                if (null != Request.QueryString["OpFavor"])
                    OperateFavorList(dataNo, empID);
            }
            return;
        }
        //如果empid不为空，就将该条的基本情况以xml输出
        if (empID != "")
        {
            bool needConfirm = false;
            bool hasConfirmed = false;
            DBOperate myDB = new DBOperate();
            myDB.SetCommandString(string.Format("Select * from ViewLog where EmpID = '{0}' and DataNo = {1} and NeedConfirm = True",empID,dataNo));
            OleDbDataReader dbReader = myDB.ExecuteReader();
            if(dbReader.Read())
            {
                needConfirm = true;
                hasConfirmed = bool.Parse(dbReader["CThisJob"].ToString());
                }
            XmlDocument xDoc = GetXmlDocument(empID, dataNo, HasRows(empID, dataNo, "FavorList"), HasRows(empID, dataNo, ""), hasConfirmed,needConfirm);
            Response.ContentType = "application/xml";
            Response.Write(xDoc.InnerXml);
        }
    }

    //用于获取对应的xml
    protected XmlDocument GetXmlDocument(string empID,string dataNo,bool isFavor,bool hasRead ,bool hasConfirmed,bool needConfirmed)
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.AppendChild(xDoc.CreateXmlDeclaration("1.0","utf-8",null));
        //创建根节点MiscInfo
        XmlElement root = xDoc.CreateElement("MiscInfo");
        xDoc.AppendChild(root);
        XmlNode xNode = xDoc.SelectSingleNode("MiscInfo");
        //设置工号
        XmlElement xe = xDoc.CreateElement("EmpID");
        xe.InnerText = empID;
        xNode.AppendChild(xe);
        //设置DataNo
        xe = xDoc.CreateElement("DataNo");
        xe.InnerText = dataNo;
        xNode.AppendChild(xe);
        //设置Favor
        xe = xDoc.CreateElement("Favor");
        xe.InnerText = isFavor.ToString();
        xNode.AppendChild(xe);
        //设置用户是否已经阅读过本条数据
        xe = xDoc.CreateElement("HasRead");
        xe.InnerText = hasRead.ToString();
        xNode.AppendChild(xe);
        //设置用户工作认认证是否已经通过
        xe = xDoc.CreateElement("HasConfirmed");
        xe.InnerText = hasConfirmed.ToString();
        xNode.AppendChild(xe);
        //设置用户是否已经申请工作认证
        xe = xDoc.CreateElement("NeedConfirm");
        xe.InnerText = needConfirmed.ToString();
        xNode.AppendChild(xe);
        return xDoc;
    }

    //用于判断某一个表中是否存在该条记录
    protected bool HasRows(string empID, string dataNo,string dtTable)
    {
        //如果传入的dtTable为空字符串，就默认查询ViewLog表中的数据
        string dTable = dtTable != "" ? dtTable : "ViewLog";
        string cmd = string.Format("select * from {2} where EmpID = '{0}' and dataNo = {1}", empID, dataNo,dTable);
        DBOperate myDB = new DBOperate();
        myDB.SetCommandString(cmd);
        OleDbDataReader dbReader = myDB.ExecuteReader();
        if (dbReader.HasRows)
        {
            myDB.CloseDB();
            return true;
        }
        else
        {
            myDB.CloseDB();
            return false;
        }
    }

    //用于更新用户访问次数
    protected void UpdateViewTime(string dataNo, string empID)
    {
        DBOperate myDB = new DBOperate();
        string cmd = string.Format("Update DataList set TotalViewTimes = TotalViewTimes+1 where [No] = {0}", dataNo);
        myDB.SetCommandString(cmd);
        myDB.ExecuteNonReader();
        myDB.CloseDB();

        if (empID != "")//如果传入的工号不为空，说明用户已经登录
        {
            myDB = new DBOperate();
            if (!HasRows(empID, dataNo,""))
            {
                //如果没有读过，就插入这个记录
                cmd = string.Format("Insert into ViewLog(EmpID,DataNo,VisitTimes,LastViewDate) values({0},{1},1,'{2}')",
                                        empID,
                                        dataNo,
                                        DateTime.Now.ToString("yyyy-MM-dd"));
                myDB.SetCommandString(cmd);
                myDB.ExecuteNonReader();
                myDB.CloseDB();
                return;

            }
            cmd = string.Format("Update ViewLog set [VisitTimes] = [VisitTimes] + 1 where [DataNo] = {0} and EmpID = '{1}'",
                                                dataNo,
                                                empID);
            myDB.SetCommandString(cmd);
            myDB.ExecuteNonReader();
            myDB.CloseDB();
        }

    }

    //用于申请工作认证
    protected void RequestConfirm(string dataNo, string empID)
    {
        //如果在ViewLog中存在记录，就将NeedConfirm设置为1
        if (HasRows(empID, dataNo, ""))
        {
            DBOperate myDB = new DBOperate();
            myDB.SetCommandString(string.Format("Update ViewLog set NeedConfirm = true where EmpID = '{0}' and dataNo = {1}", empID, dataNo));
            myDB.ExecuteNonReader();
            myDB.CloseDB();
        }
    }
    //用于添加到收藏
    protected void OperateFavorList(string dataNo, string empID)
    {
        DBOperate myDB = new DBOperate();
        //如果在FavorList中不存在此记录就将这条加入表中
        if (!HasRows(empID, dataNo, "FavorList"))
        {
            myDB.SetCommandString(string.Format("insert into FavorList(EmpID,DataNo) values({0},{1})", empID, dataNo));
        }//反之将其从表中移除
        else
        {
            myDB.SetCommandString(string.Format("Delete from FavorList where EmpID = '{0}' and DataNo = {1}", empID, dataNo));

        }
        myDB.ExecuteNonReader();
        myDB.CloseDB();
    }
}
